package com.example.harmans;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.net.Uri;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;

public class ContactActivity extends Activity {

	private TextView text_date;
    private DatePicker date_picker;
    private DatePicker thedate;
    private int year;
    private int month;
    private int dayofmonth;
    private int day;
    static final int DATE_DIALOG_ID = 100;
    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_collection);
		
		ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Contact");
        actionBar.setSubtitle("Book a Collection");

     // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);
        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_listview_item, drawerListViewItems));
 
        // App Icon 
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
 
        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
        );

        // Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true); 

        // just styling option add shadow the right edge of the drawer
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
		drawerListView.setOnItemClickListener(new DrawerItemClickListener());

		final TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
        tabs.setup();
        TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
        spec1.setContent(R.id.tab1layout);
        tabs.getTabWidget().setDividerDrawable(null);
        spec1.setIndicator("Book A Collection");
        tabs.addTab(spec1);

        TabHost.TabSpec spec2 = tabs.newTabSpec("tag2");
        spec2.setContent(R.id.tab2layout);
        spec2.setIndicator("Contact");
        tabs.addTab(spec2);
        tabs.setCurrentTab(1);

        setCurrentDate();
        addButtonListener();
        addListener();
	}

	@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }

	private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            String link = (String) ((TextView)view).getText();

            if(link.equals("Home")) {
        		Intent homeActivity = new Intent(ContactActivity.this,MainActivity.class);
        		startActivity(homeActivity);
            } else if(link.equals("Find A Court")) {
        		Intent courtActivity = new Intent(ContactActivity.this,CourtActivity.class);
		    	startActivity(courtActivity);
            } else if(link.equals("Court Fees")) {
        		Intent feesActivity = new Intent(ContactActivity.this,FeesActivity.class);
		    	startActivity(feesActivity);
            } else if(link.equals("Book A Collection")) {
        		Intent collectionActivity = new Intent(ContactActivity.this,CollectionActivity.class);
		    	startActivity(collectionActivity);
            } else if(link.equals("Interest Calculator")) {
        		Intent calcActivity = new Intent(ContactActivity.this,CalculatorActivity.class);
		    	startActivity(calcActivity);
            } else if(link.equals("FAQ")) {
        		Intent faqActivity = new Intent(ContactActivity.this,FaqActivity.class);
		    	startActivity(faqActivity);
            } else if(link.equals("Contact")) {
        		Intent contactActivity = new Intent(ContactActivity.this,ContactActivity.class);
		    	startActivity(contactActivity);
            } else if(link.equals("About")) {
    			Intent aboutActivity = new Intent(ContactActivity.this,AboutActivity.class);
		    	startActivity(aboutActivity);
            }
            drawerLayout.closeDrawer(drawerListView);
        }
    }

	public void showDatePickerDialog(View v) {
	    DialogFragment newFragment = new DatePickerFragment();
	    newFragment.show(getFragmentManager(), "datePicker");
	}
	
	public void addListener() {
		TextView book = (TextView)findViewById(R.id.book);
		book.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
				intent.setType("plain/text");
				
				EditText companyName = (EditText)findViewById(R.id.companyName);
				String companyNameStr = companyName.getText().toString();
				EditText officeLocation = (EditText)findViewById(R.id.officeLocation);
				String officeLocationStr = officeLocation.getText().toString(); 
				EditText contactName = (EditText)findViewById(R.id.contactName);
				String contactNameStr = contactName.getText().toString();
				DatePicker collectionDate = (DatePicker)findViewById(R.id.collectionDate);
				int collectionYear = collectionDate.getYear();
				int collectionMonth = collectionDate.getMonth()+1;
				int collectionDay = collectionDate.getDayOfMonth();
				String collDate = String.format("Date = %d/%d/%d",collectionDay,collectionMonth,collectionYear);

				intent.putExtra(Intent.EXTRA_EMAIL,new String[] { "casukmail@gmail.com" });
				intent.putExtra(Intent.EXTRA_SUBJECT, "New Collection Booking From Harmans Android");
				intent.putExtra(Intent.EXTRA_TEXT, "A new collction booking has been submitted via the Harmans Android Application" +"\n" +
						"Collection Date: "+collDate+"\n" +
						"Company Name: "+companyNameStr+"\n" +
						"office Location: "+officeLocationStr+"\n\n" +
						"Contact Name: "+contactNameStr);
				
				startActivityForResult(Intent.createChooser(intent, "Send FastLane Email"), 12345);
	
			}
		});
	}

	public void addListenerButton() {
		
		ImageView map1 = (ImageView)findViewById(R.id.map1);
		ImageView map2 = (ImageView)findViewById(R.id.map2);
		ImageView map3 = (ImageView)findViewById(R.id.map3);

		map1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=Ardenham+Lane+House+Aylesbury+Buckinghamshire+HP19+8AA")); 
				startActivity(i);
			}
			
		});
		
		map2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=59+New+London+Road+Chelmsford+Essex+CM2+0ND")); 
				startActivity(i);
			}
			
		});
		
		map3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=Blackwell+House+Guildhall+Yard+London+EC2V+5AE")); 
				startActivity(i);
			}
			
		});
		
		TextView call1 = (TextView)findViewById(R.id.call1);
		TextView call2 = (TextView)findViewById(R.id.call2);
		TextView call3 = (TextView)findViewById(R.id.call3);

		call1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent makeCall = new Intent(Intent.ACTION_CALL);
				makeCall.setData(Uri.parse("tel:07708503412"));
				startActivity(makeCall);
			}
		});

		call2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent makeCall = new Intent(Intent.ACTION_CALL);
				makeCall.setData(Uri.parse("tel:07708503412"));
				startActivity(makeCall);
			}
		});

		call3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent makeCall = new Intent(Intent.ACTION_CALL);
				makeCall.setData(Uri.parse("tel:07708503412"));
				startActivity(makeCall);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.collection, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home) {
			startActivity(new Intent(ContactActivity.this,MainActivity.class)); 
			return true;
		}
	    if(item.getItemId() == R.id.icon_calculator) {
	    	Intent calcIntent = new Intent(ContactActivity.this,CalculatorActivity.class);
	    	startActivity(calcIntent);
	    }
		if(item.getItemId() == R.id.menu_home) {
			Intent mainIntent = new Intent(ContactActivity.this,MainActivity.class);
			startActivity(mainIntent);
	    }
	    if(item.getItemId() == R.id.menu_court) {
	    	Intent courtIntent = new Intent(ContactActivity.this,CourtActivity.class);
	    	startActivity(courtIntent);
	    }
	    if(item.getItemId() == R.id.menu_fees) {
	    	Intent feesIntent = new Intent(ContactActivity.this,FeesActivity.class);
	    	startActivity(feesIntent);
	    }
	    if(item.getItemId() == R.id.menu_faq) {
	    	Intent faqIntent = new Intent(ContactActivity.this,FaqActivity.class);
	    	startActivity(faqIntent);
	    }
	    if(item.getItemId() == R.id.menu_contact) {
	    	Intent contactIntent = new Intent(ContactActivity.this,ContactActivity.class);
	    	startActivity(contactIntent);
	    }
	    if(item.getItemId() == R.id.menu_about) {
	    	Intent aboutIntent = new Intent(ContactActivity.this,AboutActivity.class);
	    	startActivity(aboutIntent);
	    }
	    return true;
	}
	
	public void addButtonListener() {
        thedate = (DatePicker) findViewById(R.id.collectionDate);
        thedate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
    }

	public void setCurrentDate() {
        date_picker = (DatePicker) findViewById(R.id.collectionDate);
        final Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        dayofmonth = calendar.get(Calendar.DAY_OF_MONTH);
        day = calendar.get(Calendar.DAY_OF_WEEK);
        
        long oneDayMilliseconds = 24 * 60 * 60 * 1000;
        long oneYearMilliseconds = oneDayMilliseconds * 365;
        
        Time now = new Time();
        now.setToNow();
        long today = now.toMillis(false);
        long oneyear = today + oneYearMilliseconds;

        date_picker.setCalendarViewShown(false);
        date_picker.setMinDate(today);
        date_picker.setMaxDate(oneyear);
        date_picker.init(year, month, dayofmonth, null);
    }

	
	@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
	        case DATE_DIALOG_ID:
	           return new DatePickerDialog(this, datePickerListener, year, month,day);
        }
        return null;
    }
	
	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;
            // set selected date into Text View
            text_date.setText(new StringBuilder().append(month + 1)
               .append("-").append(day).append("-").append(year).append(" "));
            // set selected date into Date Picker
            date_picker.init(year, month, day, null);
        }
    };



}
